package edu.hitsz.application;

import edu.hitsz.rankinglist.Rank;
import edu.hitsz.rankinglist.RankDaoImpl;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.LinkedList;

public class RankList {
    private JPanel rankList;
    private JLabel difficultyLabel;
    private JTable rankTable;
    private JButton deleteButton;
    private DeleteDialog deleteDialog;


    private JLabel rankListLabel;
    private JScrollPane tableScrollPane;

    private RankDaoImpl ranks;
    private String[] columnName ;
    private String[][] tableData;
    private DefaultTableModel model;
    private String diffiMode;
    private String rankFile;
    private int score;

    public static final int WINDOW_WIDTH = 512;
    public static final int WINDOW_HEIGHT = 768;



    public RankList(RankDaoImpl ranks,int modeType,int score) {
        this.score = score;
        this.ranks = ranks;
        LinkedList<Rank>  ranksData = ranks.getAllRank();
        String[] titles = {"名次","玩家名","得分","记录时间"};
        tableData = new String[ranksData.size()][4];

        for(int i=0;i<=ranksData.size()-1;++i){
            Rank r = ranksData.get(i);
            tableData[i][0] = i+1+"";
            tableData[i][1] = r.getUserName();
            tableData[i][2] = r.getScore()+"";
            tableData[i][3] = r.getTime().substring(5,16);
        }
        model = new DefaultTableModel(tableData,titles){
            @Override
            public boolean isCellEditable(int row, int col){
                return false;
            }
        };
        rankTable.setModel(model);
        if(modeType == 1){
            diffiMode = "Easy";
            rankFile = "./././rankData/rankList_easy.txt";
        }
        else if(modeType == 2){
            diffiMode = "normal";
            rankFile = "./././rankData/rankList_normal.txt";

        }
        else {
            diffiMode = "hard";
            rankFile = "./././rankData/rankList_hard.txt";
        }
        difficultyLabel.setText("难度："+diffiMode+" 您此次得分："+score);


        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(rankTable.getSelectedRow()<0){
                    return;
                }

                deleteDialog = new DeleteDialog();
                JPanel deletePanel = deleteDialog.getDeleteDialog();

                // 获得屏幕的分辨率，初始化 Frame
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                JFrame frame = new JFrame("请选择一个选项:");
                frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
                frame.setResizable(false);
                //设置窗口的大小和位置,居中放置
                frame.setBounds(((int) screenSize.getWidth() - 300) / 2, 200,
                        300, 200);
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.add(deletePanel);
                frame.setVisible(true);

                deleteDialog.nButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        frame.dispose();
                    }
                });

                deleteDialog.yButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
//                        System.out.println(rankTable.getSelectedRow());
                        int row = rankTable.getSelectedRow();
                        model.removeRow(row);
                        ranks.deleteRank(row);

                        LinkedList<Rank>  ranksData = ranks.getAllRank();
                        tableData = new String[ranksData.size()][4];

                        for(int i=0;i<=ranksData.size()-1;++i){
                            Rank r = ranksData.get(i);
                            tableData[i][0] = i+1+"";
                            tableData[i][1] = r.getUserName();
                            tableData[i][2] = r.getScore()+"";
                            tableData[i][3] = r.getTime().substring(5,16);
                        }
                        model = new DefaultTableModel(tableData,titles){
                            @Override
                            public boolean isCellEditable(int row, int col){
                                return false;
                            }
                        };
                        rankTable.setModel(model);
                        frame.dispose();
                        try{
                            File rankList = new File(rankFile);
                            // 将新数据写入数据文件
                            rankList.delete();
                            FileOutputStream fop = new FileOutputStream(rankList);
                            OutputStreamWriter writer = new OutputStreamWriter(fop, "UTF-8");
                            for(Rank r: ranksData){
                                writer.append(r.getScore()+"\r\n");
                                writer.append(r.getUserName()+"\r\n");
                                writer.append(r.getTime()+"\r\n");
                            }
                            writer.close();
                            fop.close();
                        }
                        catch (IOException e1){
                            e1.printStackTrace();
                        }
                    }
                });
            }
        });


    }

    public JPanel getPanel() {
        return rankList;
    }
    public RankDaoImpl getRanks() { return ranks; }
}
